<?php 
class bot_chat
{ 
	function chat_bot_main() 
	{
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$bot_id = $_GET['bot_id'];
		$sql = "SELECT * from `web` WHERE `web_isset` = '1';";
		$result = mysql_query($sql,$mysql_conn);
		@$row=mysql_fetch_array($result);
		$web_id = $row['web_id'];
		$web_bot = $row['web_bot'];
		$web_name = $row['web_name'];
		$web_url = $row['web_url'];
		$web_title = $row['web_title'];
		$web_keywords = $row['web_keywords'];
		$web_description = $row['web_description'];
		$web_company = $row['web_company'];
		$web_copyright = $row['web_copyright'];
		$web_isset = $row['web_isset'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_user_id = $row['bot_user_id'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		require($v_file);
	}
	function chat_get_friend() 
	{
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$bot_id = $_POST['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_user_id = $row['bot_user_id'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		$url = $server_url."openwx/get_friend_info?client=".$bot_id; 
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$friend_account = $value->{'account'};
			$friend_name = $value->{'name'};
			$friend_id = $value->{'id'};
			$friend_displayname = $value->{'displayname'};
			$friend_avatar_url = $server_url."openwx/get_avatar?id=".$friend_id."&client=".$bot_id; 
			$friend_category = $value->{'category'};
			if ($friend_category != "公众号") {
				$friend_list.= '<div class="chat-user">';
				//$friend_list.= '	<span class="pull-right label label-primary">在线</span>';
				$friend_list.= '	<img class="chat-avatar" src="'.$friend_avatar_url.'" alt="">';
				$friend_list.= '	<div class="chat-user-name">';
				$friend_list.= '		<a href="#"  onclick="addmsgobj('."'".$friend_displayname."','".$friend_id."'".')">'.$friend_displayname.'</a>';
				$friend_list.= '	</div>';
				$friend_list.= '</div>';
			}	
		}
		echo $friend_list;
	}
	function chat_get_group() 
	{
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$bot_id = $_POST['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_user_id = $row['bot_user_id'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		$url = $server_url."openwx/get_group_info?client=".$bot_id; 
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$group_displayname = $value->{'displayname'};
			$group_id = $value->{'id'};
			$group_member = $value->{'member'};
			$group_member_num = count($group_member);
			$group_avatar_url = $server_url."openwx/get_avatar?id=".$group_id."&client=".$bot_id; 			
			$group_list.= '<div class="chat-user">';
			$group_list.= '	<span class="pull-right label label-primary">成员：'.$group_member_num.' 人</span>';
			$group_list.= '	<img class="chat-avatar" src="'.$group_avatar_url.'" alt="">';
			$group_list.= '	<div class="chat-user-name">';
			$group_list.= '		<a href="#" onclick="addmsgobj('."'".$group_displayname."','".$group_id."'".')">'.$group_displayname.'</a>';
			$group_list.= '	</div>';
			$group_list.= '</div>';
		}
		echo $group_list;
	}
	function chat_get_category() 
	{
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$bot_id = $_POST['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_user_id = $row['bot_user_id'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		$url = $server_url."openwx/get_friend_info?client=".$bot_id; 
		$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			$category_account = $value->{'account'};
			$category_name = $value->{'name'};
			$category_id = $value->{'id'};
			$category_displayname = $value->{'displayname'};
			$category_avatar_url = $server_url."openwx/get_avatar?id=".$category_id."&client=".$bot_id; 
			$category_category = $value->{'category'};
			if ($category_category == "公众号") {
				$category_list.= '<div class="chat-user">';
				//$category_list.= '	<span class="pull-right label label-primary">在线</span>';
				$category_list.= '	<img class="chat-avatar" src="'.$category_avatar_url.'" alt="">';
				$category_list.= '	<div class="chat-user-name">';
				$category_list.= '		<a href="#" onclick="addmsgobj('."'".$category_displayname."','".$category_id."'".')">'.$category_displayname.'</a>';
				$category_list.= '	</div>';
				$category_list.= '</div>';
			}	
		}
		echo $category_list;
	}
	function sendmsg() 
	{
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$bot_id = $_POST['bot_id'];
		$sendmsgobj = $_POST['sendmsgobj'];
		$sendmsgbody = urlencode($_POST['sendmsgbody']);
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_user_id = $row['bot_user_id'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		$sendmsgobj_arr = explode(',',$sendmsgobj); 
		$count=count($sendmsgobj_arr); 
		for($i=0;$i<$count;$i++) 
		{ 
			$displayid = $sendmsgobj_arr[$i];
			$sendmsgobj_type = substr($displayid , 0 , 2);
			if ($sendmsgobj_type == "@@") {
				$url = $server_url."openwx/send_group_message?id=".$displayid."&content=".$sendmsgbody."&client=".$bot_id; 
			} else {
				$url = $server_url."openwx/send_friend_message?id=".$displayid."&content=".$sendmsgbody."&client=".$bot_id; 
			}
			$html = file_get_contents($url);
		}
		echo "发送成功！";		
	}
	function showmsg() 
	{
		global $mysql_conn,$v_file;
		$user_id = $_COOKIE['user_id'];
		$bot_id = $_POST['bot_id'];
		$sql = "SELECT * from `bot` WHERE `bot_id` = '".$bot_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$bot_user_id = $row['bot_user_id'];
		$bot_server_id = $row['bot_server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$bot_server_id."' ";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_url = $row['server_url'];
		$url = $server_url."openwx/check_event?client=".$bot_id; 
		@$html = file_get_contents($url);
		$contact = json_decode($html);
		foreach ($contact as $key=>$value)
		{
			//消息数据
			$msg_class = $value->{'class'};
			$msg_content = $value->{'content'};
			$msg_format = $value->{'format'};
			$msg_from = $value->{'from'};
			$msg_id = $value->{'id'};
			$msg_post_type = $value->{'post_type'};
			//媒体消息
			$msg_media_path = $value->{'media_path'};
			$msg_media_ext = $value->{'media_ext'};
			$msg_media_type = $value->{'media_type'};
			$msg_media_mime = $value->{'media_mime'};
			//如果是群会有群数据
			$msg_group = $value->{'group'};
			$msg_group_id = $value->{'group_id'};
			$msg_group_name = $value->{'group_name'};
			$msg_group_uid = $value->{'group_uid'};
			//接收方数据
			$msg_receiver = $value->{'receiver'};
			$msg_receiver_account = $value->{'receiver_account'};
			$msg_receiver_id = $value->{'receiver_id'};
			$msg_receiver_markname = $value->{'receiver_markname'};
			$msg_receiver_name = $value->{'receiver_name'};
			$msg_receiver_uid = $value->{'receiver_uid'};
			//发送方数据
			$msg_sender = $value->{'sender'};
			$msg_sender_account = $value->{'sender_account'};
			$msg_sender_id = $value->{'sender_id'};
			$msg_sender_markname = $value->{'sender_markname'};
			$msg_sender_name = $value->{'sender_name'};
			$msg_sender_uid = $value->{'sender_uid'};
			//消息数据
			$msg_time = date('Y-m-d H:i:s',$value->{'time'});
			$msg_type = $value->{'type'};
		}
		if (!empty($msg_class)) {
		$showmsgbody .='<div class="chat-message">';
		if ($msg_class == "recv") {
			if ($msg_type == "friend_message") {
				$avatar_url = $server_url."openwx/get_avatar?id=".$msg_sender_id."&client=".$bot_id; 
				$showmsgbody .='<img class="message-avatar" src="'.$avatar_url.'" alt="">';
				$showmsgbody .=' <a class="message-author" href="#" onclick="addmsgobj('."'".$msg_sender."','".$msg_sender_id."'".')"> <span class="label label-primary"> 好友：'.$msg_sender.' </span></a>';
			} else if ($msg_type == "group_message") {
				$avatar_url = $server_url."openwx/get_avatar?id=".$msg_group_id."&client=".$bot_id; 
				$showmsgbody .='<img class="message-avatar" src="'.$avatar_url.'" alt="">';
				$showmsgbody .=' <a class="message-author" href="#" onclick="addmsgobj('."'".$msg_group_name."','".$msg_group_id."'".')"> <span class="label label-success">群：'.$msg_group_name.' </span></a>';
				$showmsgbody .=' <a class="message-author" href="#" onclick="addmsgobj('."'".$msg_sender."','".$msg_sender_id."'".')"> <span class="label label-info"> 成员：'.$msg_sender.' </span></a>';
			}
			$showmsgbody .=' <a class="message-author" href="#"> <span class="label label-warning"> To </span></a>';
			$showmsgbody .=' <a class="message-author" href="#"> <span class="label label-danger"> 我 </span></a>&nbsp;&nbsp;';
			$showmsgbody .=$msg_time;
		} else if ($msg_class == "send") {
			if ($msg_type == "friend_message") {
				$avatar_url = $server_url."openwx/get_avatar?id=".$msg_sender_id."&client=".$bot_id; 
				$showmsgbody .='<img class="message-avatar" src="'.$avatar_url.'" alt="">';
				$showmsgbody .=' <a class="message-author" href="#"> <span class="label label-danger"> 我 </span></a>';
				$showmsgbody .=' <a class="message-author" href="#"> <span class="label label-warning"> To </span></a>';
				$showmsgbody .=' <a class="message-author" href="#" onclick="addmsgobj('."'".$msg_receiver."','".$msg_receiver_id."'".')"> <span class="label label-primary">好友：'.$msg_receiver.' </span></a>&nbsp;&nbsp;';
			} else if ($msg_type == "group_message") {
				$avatar_url = $server_url."openwx/get_avatar?id=".$msg_sender_id."&client=".$bot_id; 
				$showmsgbody .='<img class="message-avatar" src="'.$avatar_url.'" alt="">';
				$showmsgbody .=' <a class="message-author" href="#"> <span class="label label-danger"> 我 </span></a>';
				$showmsgbody .=' <a class="message-author" href="#"> <span class="label label-warning"> To </span></a>';
				$showmsgbody .=' <a class="message-author" href="#" onclick="addmsgobj('."'".$msg_group_name."','".$msg_group_id."'".')"> <span class="label label-success">群：'.$msg_group_name.' </span></a>&nbsp;&nbsp;';
			}
			$showmsgbody .= $msg_time;
		}
        $showmsgbody .='<div class="message">';
        $showmsgbody .='<span class="message-content">';
		if ($msg_format == "text") {
			$showmsgbody .= $msg_content;
		} else if ($msg_format == "media") {
			if ($msg_media_type == "image") {
				$showmsgbody .= '<img src="http://upload.123.com/'.$msg_media_path.'" style="width: 30%;height: 30%;">';
			} else if ($msg_media_type == "voice") {
				$showmsgbody .= '<div class="player">';
                $showmsgbody .= '<audio controls>';
                $showmsgbody .= '<source src="http://upload.123.com/'.$msg_media_path.'" type="audio/mp3">';
                $showmsgbody .= '</audio>';
                $showmsgbody .= '</div>';
			} else if ($msg_media_type == "microvideo") {
				$showmsgbody .= '<div class="player" style="width: 30%;height: 30%;">';
				$showmsgbody .= '<video controls="" name="media">';
				$showmsgbody .= '<source src="http://upload.123.com/'.$msg_media_path.'" type="video/mp4">';
				$showmsgbody .= '</video>';
				$showmsgbody .= '</div>';
			} else if ($msg_media_type == "video") {
				$showmsgbody .= '<div class="player" style="width: 30%;height: 30%;">';
				$showmsgbody .= '<video controls="" name="media">';
				$showmsgbody .= '<source src="http://upload.123.com/'.$msg_media_path.'" type="video/mp4">';
				$showmsgbody .= '</video>';
				$showmsgbody .= '</div>';
			}
		}
		//$showmsgbody .= $html;
		$showmsgbody .='</span>';
        $showmsgbody .='</div>';
        $showmsgbody .='</div>';
		echo $showmsgbody;
		}
	}
} 
/* End of file democontroller.php */ 